package com.polidea.rxohosble2.internal.scan;

import com.polidea.rxohosble2.annotation.Nullable;
import com.polidea.rxohosble2.annotation.RestrictTo;
import com.polidea.rxohosble2.ClientComponent;
import com.polidea.rxohosble2.scan.ScanFilter;
import com.polidea.rxohosble2.scan.ScanSettings;
import com.polidea.rxohosble2.internal.util.RxBleAdapterWrapper;

import java.util.ArrayList;
import java.util.List;
import bleshadow.javax.inject.Inject;
import bleshadow.javax.inject.Named;

@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
public class OhosScanObjectsConverter {

    private final int deviceSdk;

    @Inject
    public OhosScanObjectsConverter(@Named(ClientComponent.PlatformConstants.INT_DEVICE_SDK) int deviceSdk) {
        this.deviceSdk = deviceSdk;
    }

    public RxBleAdapterWrapper.LeScanSetting toNativeSettings(ScanSettings scanSettings) {
        //TODO need implement
        return null;
    }

    @Nullable
    public List<ohos.bluetooth.ble.BleScanFilter> toNativeFilters(ScanFilter... scanFilters) {
        final boolean isFilteringDefined = scanFilters != null && scanFilters.length > 0;
        final List<ohos.bluetooth.ble.BleScanFilter> returnList;
        if (isFilteringDefined) {
            returnList = new ArrayList<>(scanFilters.length);
            for (ScanFilter scanFilter : scanFilters) {
                returnList.add(toNative(scanFilter));
            }
        } else {
            returnList = null;
        }
        return returnList;
    }

    private static ohos.bluetooth.ble.BleScanFilter toNative(ScanFilter scanFilter) {
        final ohos.bluetooth.ble.BleScanFilter filter = new ohos.bluetooth.ble.BleScanFilter();
        if (scanFilter.getServiceDataUuid() != null) {
            filter.setPeripheralServiceData(scanFilter.getServiceDataUuid().getUuid(), scanFilter.getServiceData(), scanFilter.getServiceDataMask());
        }
        filter.setPeripheralAddress(scanFilter.getDeviceAddress());
        filter.setPeripheralName(scanFilter.getDeviceName());
        filter.setPeripheralManufacturerData(scanFilter.getManufacturerId(), scanFilter.getManufacturerData(), scanFilter.getManufacturerDataMask());
        if (scanFilter.getServiceUuid() != null && scanFilter.getServiceUuidMask() != null) {
            filter.setPeripheralServiceUuid(scanFilter.getServiceUuid().getUuid(), scanFilter.getServiceUuidMask().getUuid());
        }
        return filter;
    }
}
